#!/bin/tcsh -ef
####
#
#############################################################################
#                                                                           #
# Title: Prune Tiles                                                        #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 01/18/2016                                             #
# Last Modification: 01/18/2016                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 91
#
# MANUAL: This script will delete bad tiles or move them into the trash.
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
#
set imagename = ""
#
#$end_vars
#
set scriptname = 2dx_prune
\rm -f LOGS/${scriptname}.results
#
set ccp4_setup = 'y'
source ${proc_2dx}/initialize
#
echo "<<@progress: 1>>"
#
set oldir = $PWD
#
#
if ( ${cleanup_location} == "0" || ${cleanup_location} == "1" ) then
  #
  if ( ${cleanup_location} == "0" ) then
    cd ${oldir}
    cd ..
    find . -name 2dx_image.cfg -print |  grep -v "TRASH" | sed 's/\/2dx_image.cfg//' > ${oldir}/2dx_merge_alldirs.dat
    cd ${oldir}
  else
    cd ${oldir}
    cp 2dx_merge_dirfile.dat ${oldir}/2dx_merge_alldirs.dat
  endif  
  #
  set dirnum = `cat 2dx_merge_alldirs.dat | wc -l`
  echo ": Will work on ${dirnum} image directories."
  set increment = `echo ${dirnum} | awk '{ s = 70.0 / ( $1 + 1 ) } END { print s }'`
  set curval = 20.0  
  #
  set dircounter = ${dirnum}
  while ( ${dircounter} > 0 )
    #
    ${bin_2dx}/2dx_getline.exe << eot > TMP.tmp
${oldir}/2dx_merge_alldirs.dat
${dircounter}
eot
    #
    set from_dir = `cat TMP.tmp | cut -c3-`
    \rm TMP.tmp
    #
    cd ${oldir}
    cd ..
    cd ${from_dir}
    #
    set fullpath = `pwd`
    #
    ############################################################################# 
    ${proc_2dx}/linblock "Working on ${from_dir}"
    #############################################################################
    #
    set imagename = `cat 2dx_image.cfg | grep "set imagename =" | cut -d\" -f2`
    set nonmaskimagename = `cat 2dx_image.cfg | grep "set nonmaskimagename =" | cut -d\" -f2`
    #
    set PSMAX = `cat 2dx_image.cfg | grep "set PSMAX =" | cut -d\" -f2`
    if ( ${PSMAX} == "************" ) then
      set PSMAX = 0
    endif
    set MergePhaseResidual = `cat 2dx_image.cfg | grep "set MergePhaseResidual =" | cut -d\" -f2` 
    set MergePhaseRes_slot1 = `cat 2dx_image.cfg | grep "set MergePhaseRes_slot1 =" | cut -d\" -f2`
    echo "${from_dir}: ${PSMAX}, ${MergePhaseResidual}, ${MergePhaseRes_slot1}"
    set gotrash1 = `echo ${PSMAX} | awk '{ if ( $1 < 8 ) { s = 1 } else { s = 0 } } END { print s }'`
    set gotrash2 = `echo ${MergePhaseResidual} | awk '{ if ( $1 > 78.5 ) { s = 1 } else { s = 0 } } END { print s }'`
    set gotrash3 = `echo ${MergePhaseRes_slot1} | awk '{ if ( $1 > 45 ) { s = 1 } else { s = 0 } } END { print s }'`
    if ( ${gotrash1} == "1" || ${gotrash2} == "1" || ${gotrash3} == "1" ) then
      cd ..
      if ( ! -d TRASH ) then
        \mkdir TRASH
      endif
      if ( -d TRASH/${from_dir} ) then
        \rm -rf TRASH/${from_dir}
        echo "::WARNING: old TRASH/${from_dir} removed"
      endif
      echo ":: Moving into TRASH:  ${from_dir}"
      \mv ${from_dir} TRASH
    endif
    cd ${oldir}
    #
    set curval = `echo ${curval} ${increment} | awk '{ s = $1 + $2 } END { print s }'`
    set curprogress = `echo ${curval} | awk '{ s = int( $1 ) } END { print s }'`
    echo "<<@progress:${curprogress}>>"
    @ dircounter -= 1
  end
  #
endif
#
echo "<<@progress: 100>>"
#
exit
#
#
